package com.js.sys.fragment.view.fifth;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.js.lib_common.base.BasePresenter;
import com.js.lib_common.base.MvpMainFragment;
import com.js.lib_common.event.StartBrotherEvent;
import com.js.lib_common.mvp.model.response.LoginResponse;
import com.js.lib_common.utils.SPUtils;
import com.js.sys.R;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by zwz on 2020/4/22.
 */
public class UserFragment extends MvpMainFragment {


    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_version)
    TextView tvVersion;


    public static UserFragment newInstance() {
        Bundle args = new Bundle();
        UserFragment fragment = new UserFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        String json = (String) SPUtils.get(_mActivity, "LoginResponse", "");
        Gson gson=new Gson();
        LoginResponse bean=  gson.fromJson(json, LoginResponse.class);
        tvName.setText(bean.getUserVo().getName());
        tvNumber.setText(bean.getUserVo().getDepartmentName());

    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_fifith_user;
    }

    @Override
    protected void initView() {
        tvVersion.setText(getVersionName(_mActivity));

    }


    @OnClick({R.id.ll_logout,R.id.ll_about,R.id.ll_pwd,R.id.ll_work})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_logout:
                //               start(PassWordFragment.newInstance());
                ////                ((MainFragment) getParentFragment()).startBrotherFragment
                ////                        (PassWordFragment.newInstance());
//                EventBus.getDefault().post(new StartBrotherEvent(SettingsFragment.newInstance()));

                SPUtils.put(_mActivity, "isLogin", false);
                _mActivity.finish();
                break;


            case R.id.ll_about:
                EventBus.getDefault().post(new StartBrotherEvent(AboutFragment.newInstance()));
                break;
            case R.id.ll_pwd:
                EventBus.getDefault().post(new StartBrotherEvent(PWDFragment.newInstance()));
                break;
            case R.id.ll_work:
//                EventBus.getDefault().post(new StartBrotherEvent(DefaultWorkFragment.newInstance()));
                break;

        }
    }


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }






    //版本名
    public String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }



    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }

}
